﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class InsertQueue : BasePage
    {
        private Lc_QueueBLL lqb = new Lc_QueueBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Lc_BlockBLL lbb = new Lc_BlockBLL();
        private Lc_NodeBLL lnb = new Lc_NodeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProcess();

                #region 绑定审核类型

                DataTable QueueTypeData = new DataTable();
                QueueTypeData.Columns.Add("id");
                QueueTypeData.Columns.Add("name");

                QueueTypeData.Rows.Add(new object[] { "-1", "---请选择---" });
                QueueTypeData.Rows.Add(new object[] { "0", "单人审核" });
                QueueTypeData.Rows.Add(new object[] { "1", "多人审核一人通过及通过" });
                QueueTypeData.Rows.Add(new object[] { "2", "多人审核多人通过及通过" });
                QueueTypeData.Rows.Add(new object[] { "3", "指定人员办理" });
                QueueTypeData.Rows.Add(new object[] { "4", "默认片区管理员" });
                QueueTypeData.Rows.Add(new object[] { "5", "特定操作关联" });
                ddlCheckType.DataSource = QueueTypeData;
                ddlCheckType.DataValueField = "id";
                ddlCheckType.DataTextField = "name";
                ddlCheckType.DataBind();

                #endregion 绑定审核类型
            }

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void BindProcess()
        {
            DataTable table = lpb.GetAllInfo("", "");
            if (table != null && table.Rows.Count > 0)
            {
                ddlProcess.DataSource = table;
                ddlProcess.DataTextField = "C_PROCESS_NAME";
                ddlProcess.DataValueField = "N_PROCESS_ID";
                ddlProcess.DataBind();
                ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            else
            {
                MessageBox.Show(Page, "请添加流程信息");
                btnSave.Enabled = false;
                return;
            }
        }

        #region 审核方式发生改变

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCheckType.SelectedValue.Trim() != "5")
            {
                trType.Style.Add("display", "none");
            }
            else
            {
                trType.Style.Add("display", "table-row");//block
            }
        }

        #endregion 审核方式发生改变

        protected void ddlblff_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlblff.SelectedValue.Trim() == "0")
            {
                Rychang.Style["Display"] = "table-row";
                Gwchang.Style["Display"] = "None";
            }
            else
            {
                Rychang.Style["Display"] = "None";
                Gwchang.Style["Display"] = "table-row";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlProcess.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }
            if (ddlJiedian.SelectedValue.ToString() == "0")
            {
                MessageBox.Show(Page, "请选择节点");
                return;
            }
            if (ddlCheckType.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择办理方式");
                return;
            }
            if (Names.Value == "")
            {
                MessageBox.Show(Page, "请选择办理人员");
                return;
            }

            //实例化队列信息对象
            Lc_Queue model = new Lc_Queue();

            model.N_PROCESS_ID = decimal.Parse(ddlProcess.SelectedValue.ToString());

            model.N_NODE_ID = decimal.Parse(ddlJiedian.SelectedValue.ToString());

            model.N_QUEUE_NUMBER = decimal.Parse(txtZxXh.Value.ToString());
            model.C_QUEUE_TRANSACTION_ID = HIds.Value;
            if (string.IsNullOrEmpty(HIds.Value))
            {
                model.C_QUEUE_TRANSACTION_ID = "";
            }
            else
            {
                model.C_QUEUE_TRANSACTION_ID = HIds.Value;
            }

            model.N_AUDIT_TYPE = decimal.Parse(ddlCheckType.SelectedValue.ToString());
            model.C_QUEUE_BLOCK = hidBlockId.Value.ToString();
            model.C_SPICAL_TABLE = txtGLTB.Text;
            model.C_SPICAL_COL = txtGLRY.Text;
            model.N_CANCEL = 0;
            if (chkAllowZX.Checked)
            {
                model.N_CANCEL = 1;
            }

            model.N_CHANGE = 0;
            if (chkSpicaltran.Checked)
            {
                model.N_CHANGE = 1;
            }

            //判断该流程下该节点对应的队列是否已经存在
            string strC = ddlJiedian.SelectedValue.ToString() + ",";
            strC += ddlProcess.SelectedValue.ToString();
            DataTable dt = lqb.GetAllInfo("NID", strC);

            if (dt.Rows.Count > 0)
            {
                MessageBox.Show(Page, "该节点对应的队列已经存在，请重新选择");
                return;
            }

            int count = lqb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                MessageBox.Show(Page, "保存成功");
                ClearControl();
                return;
            }
            else
            {
                MessageBox.Show(Page, "保存失败");
                return;
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/BusinessExpansion/Process/QueueSelect.aspx");
        }

        private void ClearControl()
        {
            ddlProcess.SelectedValue = "-1";
            ddlJiedian.SelectedValue = "-1";
            txtZxXh.Value = "";
            Names.Value = "";
            ddlCheckType.SelectedValue = "-1";
            hidBlockId.Value = "";
            txtBlock.Value = "";
            txtGLTB.Text = "";
            txtGLRY.Text = "";
        }

        protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定流程信息
            DataTable dt = lnb.GetAllInfo("ProcessID", ddlProcess.SelectedValue);

            ddlJiedian.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    ddlJiedian.DataSource = dt;
                    ddlJiedian.DataTextField = "C_NODE_NAME";
                    ddlJiedian.DataValueField = "N_NODE_ID";
                    ddlJiedian.DataBind();
                    ddlJiedian.Items.Insert(0, new ListItem("---请选择---", "-1"));
                    btnSave.Enabled = true;
                }
            }
            else
            {
                MessageBox.Show(Page, "请添加节点信息");
                btnSave.Enabled = false;
                return;
            }
        }
    }
}